package com.ruoyi.web.controller.rest;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.WebMallProduct;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.IWebMallProductService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.*;

/**
 * @author wsliupan
 * @title: _MallProductController
 * @projectName yuansheng
 * @description: 商城列表
 * @date 2021-6-1422:10
 */
//@Api(tags = "商城")
//@RestController
//@RequestMapping("/rest/mall")
public class _MallProductController {
    @Autowired
    private IWebMallProductService iWebMallProductService;
    @Autowired
    private ISysConfigService iSysConfigService;

    @ApiOperation(value = "商品列表",notes = "数量改成销量")
    @GetMapping()
    @ApiResponses({
            @ApiResponse(code = 200, message = "返回成功",response = WebMallProduct.class),
    })
    public AjaxResult listAll() {
        IPage<WebMallProduct> ipage = new Page<WebMallProduct>(1, 10);
        IPage<WebMallProduct> data = iWebMallProductService.page(ipage,new QueryWrapper<WebMallProduct>()
                .eq("release_status","Y")
                .select("id","product_name","price","target_money","number","images","min_score")
                .orderByDesc("create_time"));
        return AjaxResult.success(data);
    }

    @ApiOperation(value = "查询首页的推荐的商品" , notes = "数量改成销量")
    @GetMapping("index")
    @ApiResponses({
            @ApiResponse(code = 200, message = "返回成功",response = WebMallProduct.class),
    })
    public AjaxResult list() {
        IPage<WebMallProduct> ipage = new Page<WebMallProduct>(1, 10);
        IPage<WebMallProduct> data = iWebMallProductService.page(ipage,new QueryWrapper<WebMallProduct>()
                .eq("release_status","Y")
                .eq("is_recommend","Y")
                .select("id","product_name","price","target_money","number","images","min_score")
                .orderByDesc("create_time"));
        return AjaxResult.success(data);
    }

    @ApiOperation(value = "详情")
    @GetMapping("detail/{productId}")
    @ApiResponses({
            @ApiResponse(code = 200, message = "返回成功",response = WebMallProduct.class),
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true),
            @ApiImplicitParam(name = "productId", value = "商品id"),
    })
    public AjaxResult detail(@PathVariable("productId") Integer productId, Integer userId) {
        WebMallProduct data = iWebMallProductService.getById(productId);
        if(StringUtils.isNotEmpty(data.getSkuStr())){
            String[] skus = data.getSkuStr().split("-");
            if(skus.length > 0){
                String skuName = skus[0];
                List<String> values = Arrays.asList(skus[1].split(","));
                List<Map<String,Object>> listMap = new ArrayList<>();
                for(String str : values){
                    Map<String,Object> map = new HashMap<>();
                    String[] val = str.split(":");
                    map.put("name",val[0]);
                    map.put("value",val[1]);
                    listMap.add(map);
                }
                Map<String,Object> mapResult = new HashMap<>();
                mapResult.put(skuName,listMap);
                String result = JSON.toJSONString(mapResult);
                data.setSkuStr(result);
            }
        }

        return AjaxResult.success(data);
    }


}
